<?php 
header("Content-Type: text/x-vCalendar");
header("Content-Disposition: inline; filename=RedJeZaterdag.vcs");

require_once 'common/common.inc.php';

$reservation_id = isset($_REQUEST["reservation_id"]) ? $_REQUEST["reservation_id"] : "";

if ($reservation_id != null && $reservation_id != "") {

  conn();
  
  $sql = "";
  $sql .= "SELECT r.ID, r.START, r.END, r.SCHEDULE_ID, r.SIGN_IN_EMAIL, CAST(r.CONFIRMED AS unsigned int) CONFIRMED, l.NAME LOCATION_NAME ";
  $sql .= "FROM LOCATIONS l ";
  $sql .= "  INNER JOIN SCHEDULES s ON s.LOCATION_CODE = l.CODE ";
  $sql .= "  INNER JOIN RESERVATIONS r ON r.SCHEDULE_ID = s.ID ";
  $sql .= "WHERE r.ID = '$reservation_id' ";
  
  //echo $sql;
  //exit;
  $result = mysql_query($sql);
  if (!$result) {die('Could not exec query: ' . mysql_error());}
  $row = mysql_fetch_array($result, MYSQL_ASSOC);
  $start = strtotime( $row["START"] );
  $end = strtotime( $row["END"] );
  $location_name = $row["LOCATION_NAME"];

  $reservation_startdate = new DateTimeDutch("@".$start, $timezone);
  $reservation_startdate->setTimezone($timezone); 
  $startdate_str = $reservation_startdate->format('Ymd\THis');
  //echo $startdate_str;
  
  $reservation_enddate = new DateTimeDutch("@".$end, $timezone);
  $reservation_enddate->setTimezone($timezone); 
  $enddate_str = $reservation_enddate->format('Ymd\THis');
  close();

  //for timezone issues
  //http://www.pcreview.co.uk/forums/importing-vcs-file-gives-wrong-times-t1809060.html
}
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//RedJeZaterdag Agenda//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTART:<?php echo $startdate_str . "\n"; ?>
DTEND:<?php echo $enddate_str . "\n"; ?>
SUMMARY:Reservering afspraak kapper (Red Je Zaterdag)
LOCATION:<?php echo $location_name . "\n"; ?>
END:VEVENT
END:VCALENDAR
